<?php
require_once 'simple_html_dom.php';

function request_by_curl(
    $remote_server,
    $post_data,
    $cookies = '',
    $nobody = false,
    $other_content = false,
    $no_phase = false
) {

    if (!$no_phase) $post_string = http_build_query($post_data);
    else $post_string = $post_data;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    if (!$other_content) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-type: application/x-www-form-urlencoded",
        ));
    } else {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $other_content);
    }
    $userAgent = ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.103.5060.114 Safari/537.36 Edg/99.103.1264.49"];
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_HEADER, true); // 返回 response header 默认 false 只会获得响应的正文
    curl_setopt($ch, CURLOPT_NOBODY, $nobody);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_COOKIE, $cookies);
    $data = curl_exec($ch);
    $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $header = substr($data, 0, $headerSize);
    $body = substr($data, $headerSize);
    $res = array();
    $res['header'] = explode("\r\n", $header);
    $res['body'] = $body;
    curl_close($ch);
    return $res;
}

function get_auth()
{
    $post_data = array(
        'log' => '1731428261@qq.com',
        'pwd' => '86964765424',
        'wp-submit' => '登录',
        'redirect_to' => 'https://bbs.inapom.com/wp-admin/',
        'testcookie' => '1'
    );
    $result = request_by_curl('https://bbs.inapom.com/wp-login.php', $post_data, $nobody = true);
    $auth_cookies = explode(";", substr($result['header'][8], 12))[0] . ';';
    $auth_cookies .= "wp-settings-10=posts_list_mode=list&ampampampampampamplibraryContent=browse&ampampampampampampmfold=o&ampampampampampampadvImgDetails=show&ampampampampampampeditor=html;";
    $auth_cookies .= "wp-settings-time-10=" . time() . ';';
    $auth_cookies .= "wordpress_test_cookie=WP Cookie check;";
    $auth_cookies .= explode(";", substr($result['header'][10], 12))[0];
    return $auth_cookies;
}

function getcode($input, $auth_cookies, $mode)
{
    $codelist = request_by_curl('https://bbs.inapom.com/wp-admin/admin.php?s=' . $input . '&page=b2_invitation_list', [], $auth_cookies);
    $html = new simple_html_dom();
    $html->load($codelist['body']);
    $list = $html->find('tbody[id=the-list]', 0);

    $id2 = $list->children[0]->children[1]->plaintext;
    $pattern = '/\d+/';
    $id1 = '';
    if (preg_match_all($pattern, $id2, $arr)) {
        $id1 .= implode($arr[0]);
    }
    $id2 = $id1;
    $code = $list->children[0]->children[2]->plaintext;
    $status = $list->children[0]->children[4]->plaintext;

    $return_arr = array();
    if ($mode == 'code') {
        if ($code != $input) {
            $id2 = '';
            $code = '';
            $status = '';
        }
    }elseif($mode != 'uid') {
        return array();
    }
    $return_arr['id'] = $id2;
    $return_arr['code'] = $code;
    $return_arr['status'] = trim($status);
    $html->clear();
    return $return_arr;
}

function generate($auth_cookies)
{
    $nonce_page = request_by_curl('https://bbs.inapom.com/wp-admin/admin.php?page=b2_invitation_bulid', [], $auth_cookies);
    $html = new simple_html_dom();
    $html->load($nonce_page['body']);
    $nonce = $html->find('input[id=nonce_CMB2phpb2_invitation_bulid_options_page]', 0)->attr['value'];
    $html->clear();
    $magic = 'uKfTC1D71LPnbCRB';
    $magic_str = '------WebKitFormBoundary' . $magic;
    $post_str = '';
    $post_data = array(
        'action' => 'b2_invitation_bulid',
        'nonce_CMB2phpb2_invitation_bulid_options_page' => $nonce,
        'invitation_code_count' => 1,
        'invitation_credit' => 1000,
        'invitation_owner' => 10,
        'submit-cmb' => '生成'
    );
    foreach ($post_data as $key => $val) {
        $post_str .= $magic_str;
        $post_str .= "\r\n";
        $post_str .= "Content-Disposition: form-data; name=\"" . $key . "\"\r\n\r\n";
        $post_str .= $val;
        $post_str .= "\r\n";
    }

    $other_content = array(
        "Content-type: multipart/form-data; boundary=----WebKitFormBoundary" . $magic,
    );
    $test = request_by_curl(
        'https://bbs.inapom.com/wp-admin/admin-post.php',
        $post_str,
        $auth_cookies,
        false,
        $other_content,
        true
    );
    //echo $test['body'];

    return getcode(10, $auth_cookies,'uid');
}

function delete_by_id($id, $auth_cookies)
{
    request_by_curl("https://bbs.inapom.com/wp-admin/admin.php?page=b2_invitation_list&action=delete&id=" . $id, [], $auth_cookies);
}

//$cookies = get_auth();
//$codes = generate($cookies);

//echo $codes['body'];
//echo '';
?>